//
//  LRPersonalViewController.m
//  本地生活圈
//
//  Created by admin on 15/8/14.
//  Copyright (c) 2015年 懒人. All rights reserved.
//

#import "LRPersonalViewController.h"
#import "NSData+Base64.h"
#import "LRExitView.h"
@interface LRPersonalViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,LRExitDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation LRPersonalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 初始化界面
    self.rightButton.hidden = YES;
    [LRFunction hidenTabBarView:YES];
    
    self.imageView.layer.cornerRadius = self.imageView.bounds.size.width / 2.0;
    self.imageView.layer.masksToBounds = YES;
    [self.imageView sd_setImageWithURL:[LRFunction getUrlWithString:[LRUserInfo shareInstance].middle_avatar]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
// UIActionSheet 8.0后过时了
- (IBAction)pushImage:(id)sender {
    UIAlertController *alertVc = [[UIAlertController alloc]init];
    
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    
    UIAlertAction *cameraAc = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:picker animated:YES completion:nil];
        }
    }];
    
    UIAlertAction *phoneAc = [UIAlertAction actionWithTitle:@"手机相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:picker animated:YES completion:nil];
        }
    }];
    
    UIAlertAction *cancelAc = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        return ;
    }];
    
    [alertVc addAction:cameraAc];
    [alertVc addAction:phoneAc];
    [alertVc addAction:cancelAc];
    [self presentViewController:alertVc animated:YES completion:nil];
}

#pragma mark - UIImagePickerController 代理方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = info[@"UIImagePickerControllerOriginalImage"];
    self.imageView.image = image;
    [self pushDataWithImage:image];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

// 上传修改头像
- (void)pushDataWithImage:(UIImage *)image
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:[[LRUserInfo shareInstance] user_id] forKey:@"user_id"];
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    [param setObject:[data base64Encoding] forKey:@"avatar"];
    [param setObject:[LRFunction getTimeScamp] forKey:@"time"];
    NSArray *array = @[APP_ID,param[@"user_id"],param[@"time"],APP_IDENGER];
    [param setObject:[LRFunction md5StringFromArray:array] forKey:@"sign"];
    [param setObject:APP_ID forKey:@"app_id"];
    [self showLoadingView];
    
    // 发送请求
    NSString *urlStr = @"http://www.qd-life.com/index.php?anu=api/1/user/update_avatar";
    [self.manager POST:urlStr parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 刷新数据
        [LRUserInfo shareInstance].small_avatar = responseObject[@"small_avatar"];
        [LRUserInfo shareInstance].middle_avatar = responseObject[@"middle_avatar"];
        // 保存到沙盒
        NSMutableDictionary *dict = (NSMutableDictionary *)[LRFunction getUserInfo];
        dict[@"small_avatar"] = responseObject[@"small_avatar"];
        dict[@"middle_avatar"] = responseObject[@"middle_avatar"];
        [LRFunction saveUserInfoWithDict:dict];
        [self hidenLoadingView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD showError:@"上传失败"];
    }];
}

- (IBAction)changeInfo {
}

- (IBAction)changePwd {
}

// 注销用户
- (IBAction)exitClick {
    LRExitView *exit = [[NSBundle mainBundle] loadNibNamed:@"LRExitView" owner:nil options:nil].lastObject;
    exit.backgroundColor = [UIColor clearColor];
    exit.delegate = self;
    [self.view addSubview:exit];
}

#pragma mark - LRExitViewDelegate

- (void)didExitView:(UIView *)view
{
    [view removeFromSuperview];
    
    // 删除数据
    LRUserInfo *user = [LRUserInfo shareInstance];
    user.isLogin = NO;
    // 忘记密码
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kRememberPwd];
    
    [self backClick];
}

- (void)didCancelExitView:(UIView *)view
{
    [view removeFromSuperview];
}



@end
